package com.sjx.onelinecord

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


/**
 * 使用SharePreferences存储用户名、用户密码、用户记住密码复选框状态。
 */

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val prefs = getPreferences(Context.MODE_PRIVATE)
        var isRemember: Boolean = prefs.getBoolean("isRemember", false)
        if (isRemember) {
            val account = prefs.getString("account", "")
            val password = prefs.getString("password", "")
            accountEdit.setText(account)
            passwordEdit.setText(password)
            rememberPass.isChecked = true
        }

        login.setOnClickListener {
            val edit = prefs.edit()
            var account = accountEdit.text.toString()
            var password = passwordEdit.text.toString()
            val isRemember: Boolean = rememberPass.isChecked
            if (account == "sjx" && password == "196819") {
                if (rememberPass.isChecked) {
                    edit.putString("account", account)
                    edit.putString("password", password)
                    edit.putBoolean("isRemember", isRemember)
                } else {
                    edit.clear()
                }
                edit.apply()
            } else {
                Toast.makeText(this, "account or password is invalid",
                        Toast.LENGTH_LONG).show()
            }
        }
    }
}